package com.hc.serviceimpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.entity.Refrigeratormv;
import com.hc.mapper.RefrigeratormvMapper;
import com.hc.model.AvgTempModel;
import com.hc.service.IRefrigeratormvService;
import com.hc.util.EquipmentRecordServiceImplUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuZhiHao
 * @since 2019-12-04
 */
@Service
@Slf4j
public class RefrigeratormvServiceImpl extends ServiceImpl<RefrigeratormvMapper, Refrigeratormv> implements IRefrigeratormvService {

    @Autowired
    private RefrigeratormvMapper refrigeratormvMapper;


    /*此方法获取日的所有冰箱的温度平均值*/
    @Override
    public Map<String, List<AvgTempModel>> selectTemp(String date) {
        List<Refrigeratormv> refrigeratormvlist = refrigeratormvMapper.selectfrigerator(date);
        Map<String, List<Refrigeratormv>> map = refrigeratormvlist.stream().collect(Collectors.groupingBy(Refrigeratormv::getEquipmentName));
        Map<String, List<AvgTempModel>> map1 = new HashMap<>();
        map.forEach((k,v)->{
            List<AvgTempModel> list = new ArrayList<>();
            v.forEach(p->{
                AvgTempModel avgTempModel = new AvgTempModel();
                avgTempModel.setMean_value(p.getMeanValue());
                avgTempModel.setTimeInterval(p.getTimeInterval());
                avgTempModel.setInput_time(p.getInputTime());
                list.add(avgTempModel);
            });
            map1.put(k,list);
        });
        return map1;
    }

   /* //此方法获取一个冰箱一个月的平均值
   * @Parma equipment  冰箱名称
   * @ parm startTime 开始时间
   * @parm endtime  结束时间*/
    @Override
    public Map<String, List<AvgTempModel>> selectTempmonth(String equipment, String startTime, String endTime) {
        List<Refrigeratormv> selectfrigeratormonth = refrigeratormvMapper.selectfrigeratormonth(equipment, startTime, endTime);
        Map<String, List<Refrigeratormv>> collect = selectfrigeratormonth.stream().collect(Collectors.groupingBy(r -> r.getInputTime().substring(0,10)));
        List<String> daylist = EquipmentRecordServiceImplUtil.timeDifferenceCount(startTime, endTime);
        Map<String, List<AvgTempModel>> mapmonth = new HashMap<>();
        for (int i = 0; i < daylist.size(); i++) {
            ArrayList<AvgTempModel> listavgtempModel = new ArrayList<>();
            for (int j = 1; j < 9; j++) {
                AvgTempModel avgTempModel = new AvgTempModel();
                //设置平均值对应的时间属性
                avgTempModel.setTimeInterval(j+"");
                listavgtempModel.add(avgTempModel);
            }
            String day = daylist.get(i);
            List<Refrigeratormv> refrigeratormvs = collect.get(day);

            if (CollectionUtils.isNotEmpty(refrigeratormvs)) {
                refrigeratormvs.forEach(rf -> {
                    String timeInterval = rf.getTimeInterval();
                    Double meanValue = rf.getMeanValue();
                    for (int m = 0; m < 8; m++) {
                        if(timeInterval.equals(listavgtempModel.get(m).getTimeInterval())){
                            listavgtempModel.get(m).setMean_value(meanValue);
                        }
                    }
                });
            }
            mapmonth.put(day, listavgtempModel);
        }
        return mapmonth;
    }

}
